package com.hfi.alibaba.java8datatimeapi;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author ChangLiang
 * @date 2019/6/30
 */
public class Java8TimeTest {

    /**
     * LocalDate 没有时区的 本地的日期
     */
    @Test
    public void test(){
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);

        // getMonth  April这种方式
        // getMonthValue 从1-12
        System.out.println(localDate.getYear() + ", " + localDate.getMonthValue() + ", " + localDate.getDayOfMonth());

        LocalDate localDate1 = LocalDate.of(2017, 3, 3);
        System.out.println(localDate1);

        // 两周后是哪一天
        LocalDate localDate2 = localDate.plus(2, ChronoUnit.WEEKS);
        System.out.println(localDate2);

        // 判断当前日期是不是在指定日期之前 指定日期之后
        System.out.println(localDate1.isBefore(localDate2));
    }

    /**
     * MonthDay
     */
    @Test
    public void test1(){
        LocalDate localDate1 = LocalDate.of(2017, 3, 3);

        // 到了哪一年的几月几号 如生日
        MonthDay monthDay = MonthDay.of(localDate1.getMonth(), localDate1.getDayOfMonth());
        MonthDay monthDay1 = MonthDay.from(LocalDate.of(2018, 3, 3));
        // 月与日是不是同一天
        if (monthDay.equals(monthDay1)) {
            System.out.println("equals");
        } else {
            System.out.println("not equals");
        }
    }

    /**
     * LocalTime 没有时区的 本地的时间
     */
    @Test
    public void test2() {
        LocalTime time = LocalTime.now();
        System.out.println(time);

        LocalTime localTime = time.plusHours(3).plusMinutes(20);
        System.out.println(localTime);
    }

    /**
     * ModelA clock providing access to the current instant
     * instant就代表时间线上的某个时间点
     */
    @Test
    public void test3() {
        Clock clock = Clock.systemDefaultZone();
        System.out.println(clock.millis());
    }

    /**
     * ZoneId 时区信息
     */
    @Test
    public void test4() {
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        // 把set放到treeSet中进行排序
        // treeSet直接指向一个子类的对象
        // 里面的{}表示是一个构造块 表示这个实例在创建的时候 构造块里面的代码会自动的执行
        Set<String> treeSet = new TreeSet<String>() {
            {
                addAll(zoneIds);
            }
        };

        // 另一种写法
        TreeSet<String> treeSet1 = new TreeSet<>();
        treeSet1.addAll(zoneIds);

//        treeSet.stream().forEach(System.out::println);


        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);

        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
        System.out.println(zonedDateTime);
    }

    /**
     * YearMonth
     */
    @Test
    public void test5(){
        YearMonth yearMonth = YearMonth.now();
        System.out.println(yearMonth);
        System.out.println(yearMonth.lengthOfMonth());
        System.out.println(yearMonth.isLeapYear());

        System.out.println("-----------------");

        YearMonth yearMonth1 = YearMonth.of(2016, 2);
        System.out.println(yearMonth1);
        System.out.println(yearMonth1.lengthOfMonth());
        System.out.println(yearMonth1.lengthOfYear());
        System.out.println(yearMonth1.isLeapYear());
    }

    /**
     * such as '2 years, 3 months and 4 days'
     */
    @Test
    public void test6() {
        LocalDate localDate = LocalDate.now();
        LocalDate localDate1 = LocalDate.of(2019, 8, 2);
        Period period = Period.between(localDate, localDate1);
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
        // output 间隔1个月零3天
    }

    /**
     * Instant 是以UTC不带时区方式表示的
     */
    @Test
    public void test7() {
        System.out.println(Instant.now());
    }


    /**
     * Duration
     */
    @Test
    public void test8(){
        LocalTime localTime = LocalTime.now();
        LocalTime localTime1 = localTime.minusHours(11);
        Duration duration = Duration.between(localTime, localTime1);
        System.out.println(duration.toHours());
    }

    /**
     * DateTimeFormatter
     */
    @Test
    public void test9() {
        LocalDate localDate = LocalDate.now();
        String format = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
        String format1 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
        System.out.println(format);
        System.out.println(format1);

        System.out.println("----------");

        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String format2 = localDateTime.format(dateTimeFormatter);
        System.out.println(format2);

        System.out.println("----------");

        LocalDate localDate1 = LocalDate.parse("20161112", DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println(localDate1);
    }
}
